import { Chart } from '@antv/g2';
import { useEffect } from 'react';
import JSONEditor from "@/pages/antv/components/JSONEditorComponent";
import { useState } from 'react';
import { Button } from 'antd';

export default function antvPage() {
  const option = {
    x: "x",
    y: "y",
    data: [
      { x: 'Sports', y: 275 },
      { x: 'Strategy', y: 115 },
      { x: 'Action', y: 120 },
      { x: 'Shooter', y: 350 },
      { x: 'Other', y: 150 },
    ]
  }
  //let chart: any;
  let [data, setData] = useState(option);

  function showData() {
    // 初始化图表实例
    let chart = new Chart({
      container: 'container',
      autoFit: true,
    });
    console.log(data, "data");
    console.log(chart, "chart");
    chart
      .interval()
      .coordinate({ transform: [{ type: 'transpose' }] })
      .data(data.data)
      .encode('x', data.x)
      .encode('y', data.y);

    // 渲染可视化
    chart.render();
  }


  // 准备数据
  useEffect(() => {
    showData()
    return () => {
      //chart.destroy();
    }
  }, [])

  return (
    <div>
      <div id="container" ></div>
      <Button onClick={() => {
        showData()
      }}> 提交 </Button>
      <JSONEditor option={data} onDataChange={(e) => {
        setData(e.json)
      }}></JSONEditor>
    </div>
  );
}

antvPage.route = {
  [MENU_TITLE]: "简单条形图",
  [MENU_PATH]: "/antv/g2/t2",
};
